package com.zh.controller;

import com.zh.entity.User;
import com.zh.service.UserService;
import com.zh.util.dict.CodeMsg;
import com.zh.util.dict.Validator;
import com.zh.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.Pattern;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author wiki
 * @since 2018-03-19
 */
@RestController
@RequestMapping("/user")
@Validated
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    @PostMapping("/modifyPassword")
    public Result modifyPassword(User user,
                                 @Pattern(regexp = Validator.REGEX_PASSWORD, message = "旧密码格式错误")
                                 String oldPassword,
                                 @Pattern(regexp = Validator.REGEX_PASSWORD, message = "新密码格式错误")
                                 String newPassword){
        //TODO remembor password add salt
        if(!user.getPassword().equals(oldPassword)){
            return Result.failure(CodeMsg.OLD_PASSWORD_ERROR);
        }

        user.setPassword(newPassword);
        userService.updateById(user);
        return Result.success();
    }

    @PostMapping("/info")
    public Result info(User user){
        return Result.success(user);
    }

}
